﻿// PackageTest.cs
//
// Copyright 2008 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//
using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using YaTools.Yaml.LanguagePackage;

namespace IntegrationTestProject
{
    /// <summary>
    /// Integration test for package validation
    /// </summary>
    [TestClass]
    public class PackageTest
    {
        private delegate void ThreadInvoker();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestMethod]
        [HostType("VS IDE")]
        public void PackageLoadTest()
        {
            UIThreadInvoker.Invoke((ThreadInvoker) delegate()
            {

                //Get the Shell Service
                IVsShell shellService = VsIdeTestHostContext.ServiceProvider.GetService(typeof(SVsShell)) as IVsShell;
                Assert.IsNotNull(shellService);

                object value;
                CheckHR(shellService.GetProperty((int)__VSSPROPID.VSSPROPID_VirtualRegistryRoot,out value));


                TestContext.WriteLine("Registry root: {0}", value); 
                
                //Validate package load
                IVsPackage package;
                Guid packageGuid = Marshal.GenerateGuidForType(typeof(YamlLanguagePackage));

                int installed = 0;
                CheckHR(shellService.IsPackageInstalled(ref packageGuid, out installed));
                Assert.AreEqual(1, installed, "Package not installed");

                CheckHR(shellService.LoadPackage(ref packageGuid, out package));
                Assert.IsNotNull(package, "Package failed to load");
            });
        }

        private void CheckHR( int hResult )
        {
            if (hResult >= 0) 
                return;
            Marshal.ThrowExceptionForHR(hResult);
        }
    }
}
